#!/bin/bash
#修改时间 2022-11-01
date_stamp=`date "+%Y-%m-%d %H:%M:%S"`
date1=`date "+%Y-%m-%d-%H-%M-%S"`
echo "123+123" 
branch_v=$1
user_id=$2
build_id=$3
status=$4
roll_num=$5
echo ${roll_num} ${status} 
#jar地址
SERVCER_JAR=dianshu-backend-official-1.0.jar
#日志名称
LOG_NAME=dianshu-backend.log
#进入对应目录
DIR="/home/ubuntu/dianshu-backend"
#输出信息
echo '---------------------------------------'
echo $DIR
#创建项目jar包工程目录
if [ ! -d $DIR/jar ];then
  mkdir -p $DIR/jar
fi
#创建备份目录
echo $date_stamp
if [ ! -d $DIR/bak/$build_id ];then
  mkdir -p $DIR/bak/$build_id
fi
#备份
if [ ! -f "$DIR/jar/$SERVCER_JAR" ];then
  echo $SERVCER_JAR"文件不存在"
else
  cp $DIR/jar/$SERVCER_JAR  $DIR/bak/$build_id/
fi
#杀死java进程
_pid=`ps -ef | grep java | grep $SERVCER_JAR |awk '{print $2}'`
echo "kill ------>"$SERVCER_JAR "pid---->" $_pid
[ -n "$_pid" ] && kill -9 $_pid
#删除老包，替换新包
#rm -rf $DIR/jar/$SERVCER_JAR
#rm -rf $DIR/$SERVCER_JAR
cp $DIR/$SERVCER_JAR  $DIR/jar
export JAVA_HOME=/usr/lib/jdk/jdk1.8.0_311
export JAVA=$JAVA_HOME/bin/java
export PATH=${JAVA_HOME}/bin:$PATH
echo '---------------------------------------java path'
#source /home/ubuntu/backend/.env
########### 判断部署还是回滚 ###############################
if [ "$status" = "Deploy" ]; then
  echo "开始正常部署"
  nohup  java -jar \
          -Xms256m -Xmx256m -XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=128M -XX:+UseG1GC \
          -Xloggc:$DIR/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps \
          -Dspring.datasource.username=dianshu -Dspring.datasource.password='u%7mZqw)#tiy2tR' \
          -Dtencent.secret-id=AKIDe9ON6ZlhLksjJdhU1II2UfbHfmdDVXJo \
          -Dtencent.secret-key=FEwXrugCRP6YjdGdjY6PSEjxvc2A3f04 \
          -Dspring.mail.username=contact@yeez.tech \
          -Dspring.mail.password=vDuNregMaDzXBic2 \
          -Dlogging.file.path=$DIR \
          -Dserver.address=127.0.0.1 \
          $DIR/jar/dianshu-backend-official-1.0.jar  >>$DIR/logs/$LOG_NAME  2>&1  &
  sleep 15
  s_pid=`ps -ef | grep java | grep $SERVCER_JAR |awk '{print $2}'`
  send_weixin_v2  "【部署推送】\n【$date_stamp】\n【典枢】\n【backend/后端/】\n【典枢Demo环境】【fenglin测试环境/43.138.115.87】\n【$branch_v】 分支部署完成, 进程pid：$s_pid ,由 $2 用户构建部署。 "
elif [ -d $DIR/bak/$roll_num  ] && [ -f $DIR/bak/$roll_num/$SERVCER_JAR ]; then
    echo "开始回滚操作"
    rm -rf $DIR/jar/$SERVCER_JAR;
    cp  $DIR/bak/$roll_num/$SERVCER_JAR  $DIR/jar/ ;
        echo " 拷贝回滚版本到启动目录！开始启动"
        nohup  java -jar \
          -Xms256m -Xmx256m -XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=128M -XX:+UseG1GC \
          -Xloggc:$DIR/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps \
          -Dspring.datasource.username=dianshu -Dspring.datasource.password='u%7mZqw)#tiy2tR' \
          -Dtencent.secret-id=AKIDe9ON6ZlhLksjJdhU1II2UfbHfmdDVXJo \
          -Dtencent.secret-key=FEwXrugCRP6YjdGdjY6PSEjxvc2A3f04 \
          -Dspring.mail.username=contact@yeez.tech \
          -Dspring.mail.password=vDuNregMaDzXBic2 \
          -Dlogging.file.path=$DIR \
          -Dserver.address=127.0.0.1 \
          $DIR/jar/dianshu-backend-official-1.0.jar  >>$DIR/logs/$LOG_NAME  2>&1  &
  sleep 15
  s_pid=`ps -ef | grep java | grep $SERVCER_JAR |awk '{print $2}'`
  send_weixin_v2  "【回滚推送】\n【$date_stamp】\n【典枢】\n【backend/后端/】\n【典枢Demo环境】【fenglin测试环境/43.138.115.87】\n【$branch_v】 分支部署完成,回滚版本为$roll_num 进程pid：$s_pid ,由 $2 用户构建部署。 "
fi
